package 剑指Offer;

public class Offer57_和为s的两个数字 {
    public int[] twoSum(int[] nums, int target) {
        if (nums.length == 0) {
            return new int[]{};
        }
        int n = nums.length;
        int left = 0;
        int right = n - 1;

        while (left <= right) {
            //不可以自己加自己，所以left==right的时候就要跳出循环
            if (left == right) {
                break;
            }
            int sum = nums[left] + nums[right];
            if (sum == target) {
                return new int[]{nums[left], nums[right]};
            } else if (sum > target) {
                right--;
            } else if (sum < target) {
                left++;
            }

        }
        return new int[]{};
    }
}
